<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */ 
namespace Mobile\Controller;
class IndexController extends MobileBaseController {

    public function index(){


        //获取微信配置
        $wechat_list = M('wx_user')->select();
        $wechat_config = $wechat_list[0];
        $this->weixin_config = $wechat_config;
        // 微信Jssdk 操作类 用分享朋友圈 JS
        $jssdk = new \Mobile\Logic\Jssdk($this->weixin_config['appid'], $this->weixin_config['appsecret']);
        $signPackage = $jssdk->GetSignPackage();
        $user=$_SESSION['user'];
        $this->assign('user',$user);
        $hot_goods = M('goods')->where("is_hot=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页热卖商品
        $thems = M('goods_category')->where('level=1')->order('sort_order')->limit(9)->cache(true,TPSHOP_CACHE_TIME)->select();
        $this->assign('thems',$thems);
        $this->assign('hot_goods',$hot_goods);
        $favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
        $this->assign('favourite_goods',$favourite_goods);
        $this->assign('signPackage',$signPackage);
		
            $this->assign('navid',"home"); //存储用户信息

        //在线人数统计

        $a_time = strtotime(date('Y-m-d', time()));
        $e_time = 60*60*24+$a_time;

        //当天进入开门的所有数量
        $j_where['cate_id']=1;
        $j_where['add_time']=array(array('lt',$e_time),array('gt',$a_time),'and');
        $j_num =M('open_door_log')->where($j_where)->count();
        //  当天出去开门的所有数量
        $j_where['cate_id']=2;
        $j_num =M('open_door_log')->where($j_where)->count();


        $on_live_num=$j_num-$j_num;
        if($on_live_num<16)
            $on_live_num=rand(16,20);

        $this->assign('on_live_num',$on_live_num);

        $this->display();
    }

    /**
     * 进入开门
     */
     public function begin_open(){
            $user_id=$_SESSION['user']['user_id'];
            $user=M('users')->where("user_id=$user_id")->find();
            if(!$user)
                $this->error('用户不存在！');
            $coach=M('coach')->where("user_id=$user_id")->find();
            if($coach){
                //教练 直接执行开门
                open_door($user_id,1,4);
            }else {   //普通用户



                $today = date('Ymd');
                if ($user['is_member'] && $today < $user['member_endtime']) {//特殊会员(并在有效期内)
                    $res = open_door($user_id, 1, 1);
                } elseif ($user['once_num'] > 0) {//次卡用户（并且次数大于0）
                    $res = open_door($user_id, 1, 2);
                } elseif ($user['old_member_endtime'] > $today) {//旧用户免费期内开门
                    $res = open_door($user_id, 1, 6);
                } else {
                    $o_where['user_id'] = $user_id;
                    $o_where['type_id'] = 2;
                    $o_where['pay_status'] = 0;
                    $o_where['order_status'] = 0;
                    $n_num = M('order')->where($o_where)->count();
                    if ($n_num > 0){
                        $arr['status']=-1;
                        $arr['msg']='您有未支付订单，支付后方可进入';
                        exit(json_encode($arr));
                    }
                    $data['user_id'] = $user_id;
                    $data['date'] = date("Ymd");
                    $data['start_time'] = time();
                    $data['add_time'] = time();
                    $data['mobile'] = $user['mobile'];
                    $data['order_sn'] = getrand_num();
                    $data['type_id'] = 2;
                    $data['end_time'] = 0;


                    $order_id = M('order')->add($data);

                    if ($order_id) {
                        $res = open_door($user_id, 1, 3);//普通时常订单用户开门
                    } else {
                        $arr['status']=0;
                        $arr['msg']='开门失败，请联系管理员！';
                        exit(json_encode($arr));
                    }
                }
                if ($res->code == '0') {
                    $arr['status']=1;
                    $arr['msg']='欢迎光临';
                    exit(json_encode($arr));
                } else {
                    $arr['status']=0;
                    $arr['msg']='开门失败，请联系管理员！';
                    exit(json_encode($arr));
                }
            }
         }

    /**
     * 结束开门
     */

    public function end_open(){

//        update_pay_status('2018042511155513636');exit;



        $user_id=$_SESSION['user']['user_id'];
        $user=M('users')->where("user_id=$user_id")->find();
        if(!$user)
            $this->error('用户不存在！');
        $coach=M('coach')->where("user_id=$user_id")->find();
        if($coach){
            //教练 直接执行开门
            open_door($user_id,2,4);
        }else {   //普通用户

            $today = date('Ymd');
            if ($user['is_member'] && $today < $user['member_endtime']) {//特殊会员(并在有效期内)
                $res = open_door($user_id, 2, 1);
            } elseif ($user['once_num'] > 0) {//次卡用户（并且次数大于0）
                $res = open_door($user_id, 2, 2);
            } elseif ($user['old_member_endtime'] > 0) {//旧用户免费期内开门
                $res = open_door($user_id, 2, 6);
            } else {
                //订单支付
                $where['user_id']=$user_id;
                $where['pay_status']=0;
                $where['date']=date("Ymd");
                $order=M('order')->order('start_time DESC')->where($where)->find();


                //计算订单价格
                if($order){


                    $min_num =(time()-$order['start_time'])/60;//进入的分钟数
                    $config = tpCache('charge_info');
                    $minute_charge=$config['minute_charge'];//配置的每分钟收费单价
                    $upper_charge=$config['upper_charge'];//单日封顶价
                    $order_money=$min_num*$minute_charge;//生成订单的总价
                    $order_money>$upper_charge?$order_amount=$upper_charge:$order_amount=$order_money;
                    $order_id=$order['order_id'];
                    $up_data['order_id']=$order_id;
                    $up_data['order_amount']=$order_amount;
                    $up_data['end_time']=time();
                    $res=M('order')->save($up_data);
                    $order=M('order')->where("order_id=$order_id")->find();

                    include_once  "plugins/payment/weixin/weixin.class.php";
                    $weixin = new \weixin();

                    $return =$weixin->getJSAPI($order);

                    exit($return);

                }else{
                    $arr['status']=0;
                    $arr['msg']='非法进入，开门失败，请联系管理员！';
                    exit(json_encode($arr));
                }




/*
                $arr['status']=0;
                $arr['msg']=$weixin;
                exit(json_encode($arr));
                $o_where['user_id'] = $user_id;
                $o_where['end_time'] = 0;
                $n_num = M('order')->where($o_where)->count();
                if ($n_num > 0){
                    $arr['status']=-1;
                    $arr['msg']='您有未支付订单，请与管理员联系';
                    exit(json_encode($arr));
                }
                $data['user_id'] = $user_id;
                $data['start_time'] = time();
                $data['add_time'] = time();
                $data['mobile'] = $user['mobile'];
                $data['order_sn'] = getrand_num();
                $data['type_id'] = 2;
                $data['end_time'] = 0;
                $order_id = M('order')->add($data);
                if ($order_id) {
                    $res = open_door($user_id, 2, 3);//普通时常订单用户开门
                } else {
                    $arr['status']=0;
                    $arr['msg']='开门失败，请联系管理员！';
                    exit(json_encode($arr));
                }*/
            }
           /* if ($res->code == '0') {
                $arr['status']=1;
                $arr['msg']='欢迎光临';
                exit(json_encode($arr));
            } else {
                $arr['status']=0;
                $arr['msg']='开门失败，请联系管理员！';
                exit(json_encode($arr));
            }*/
        }













    }


    public function open_test(){


        $user_id =I('user_id');

        $user=M('users')->where("user_id=$user_id")->find();

        if(!$user)
            $this->error('用户不存在！');
        $coach=M('coach')->where("user_id=$user_id")->find();


        if($coach){
            //教练 直接执行开门
            open_door($user_id,1,4);
        }else{   //普通用户

            $today=date('Ymd');
            if($user['is_member']&&$today<$user['member_endtime']){//特殊会员(并在有效期内)
               $res= open_door($user_id,1,1);
            }elseif($user['once_num']>0){//次卡用户（并且次数大于0）
                $res = open_door($user_id,1,2);
            }elseif($user['old_member_endtime']>0){//旧用户免费期内开门
                $res = open_door($user_id,1,6);
            }else{

                $o_where['user_id']=$user_id;
                $o_where['end_time']=0;

                $n_num=M('order')->where($o_where)->count();

                if($n_num>0)
                    $this->error('您有未支付订单，请与管理员联系');

                $data['user_id']=$user_id;
                $data['start_time']=time();
                $data['add_time']=time();
                $data['mobile']=$user['mobile'];
                $data['order_sn']=getrand_num();
                $data['type_id']=2;
                $data['end_time']=0;
                $order_id=M('order')->add($data);
                if($order_id){
                   $res = open_door($user_id,1,3);//普通时常订单用户开门
                }else{
                    $this->error('开门失败，请联系管理员！');exit;
                }
            }

            if($res->code=='0'){
                $this->success('欢迎光临',U('User/index'));exit;
            }else{
                $this->error('开门失败，请联系管理员！');exit;
            }



        }



      /*  $url ="http://api.parklinesms.com/service.php";
        $data['apikey']='705303165efdb2ce6b306e5052e93c10';
        $data['deviceid']='210654';
        $data['command']='01';
        $data['lockid']='01';
        $return = httpRequest($url,'POST',$data);
        $return = json_decode($return);
        $return=json_decode($return);
        $this->success('开锁成功','',3);*/
//        if($return['msg']=='success'){
//
//        }else{
//            $this->success('开锁成功');
//        }
//

    }






    public function enter_open(){
        $open_door_config =C('open_door');
        $code= $open_door_config['verifyKey'];
        $res =open_door($code,1);
        if($res){
            $this->success('开锁成功','',3);
        }else{
            $this->error('开锁失败','',3);
        }
    }

    public function out_open(){
        $open_door_config =C('open_door');
        $code= $open_door_config['verifyKey'];
        $res =open_door($code,2);
        if($res){
            $this->success('开锁成功','',3);
        }else{
            $this->error('开锁失败','',3);
        }
    }


    /**
     * 分类列表显示
     */
    public function categoryList(){
        $this->display();
    }

    /**
     * 模板列表
     */
    public function mobanlist(){
        $arr = glob("D:/wamp/www/svn_tpshop/mobile--html/*.html");
        foreach($arr as $key => $val)
        {
            $html = end(explode('/', $val));
            echo "<a href='http://www.php.com/svn_tpshop/mobile--html/{$html}' target='_blank'>{$html}</a> <br/>";            
        }        
    }
    
    /**
     * 商品列表页
     */
    public function goodsList(){
        $goodsLogic = new \Home\Logic\GoodsLogic(); // 前台商品操作逻辑类
        $id = I('get.id',0); // 当前分类id
        $lists = getCatGrandson($id);
        $this->assign('lists',$lists);
        $this->display();
    }
    
    public function ajaxGetMore(){
    	$p = I('p',1);
    	$favourite_goods = M('goods')->where("is_on_sale=1")->order('goods_id asc,sort desc')->page($p,10)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
    	$this->assign('favourite_goods',$favourite_goods);
    	$this->display();
    }
}